package com.cd.university.controller;

import com.alibaba.fastjson.JSONObject;
import com.cd.university.common.RestResult;
import com.cd.university.db.pojo.dictionary.*;
import com.cd.university.service.impl.DictionaryProcessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 陈栋
 * @create 2021/11/6 18:50
 */
@RestController
@RequestMapping("/dictionary")
@Api("字典数据接口")
public class DictionaryController {

    @Autowired
    private DictionaryProcessor dictionaryProcessor;

    private static final Map<String, Class> POJO_CLASS_MAP = new HashMap<>();

    static {
        POJO_CLASS_MAP.put("achievementType", AchievementType.class);
        POJO_CLASS_MAP.put("conclusionType", ConclusionType.class);
        POJO_CLASS_MAP.put("department", Department.class);
        POJO_CLASS_MAP.put("level",Level.class);
        POJO_CLASS_MAP.put("nature", Nature.class);
        POJO_CLASS_MAP.put("paperType", PaperType.class);
        POJO_CLASS_MAP.put("patentRange",PatentRange.class);
        POJO_CLASS_MAP.put("patentStatus",PatentStatus.class);
        POJO_CLASS_MAP.put("periodical",Periodical.class);
        POJO_CLASS_MAP.put("researchType",ResearchType.class);
        POJO_CLASS_MAP.put("status",Status.class);
        POJO_CLASS_MAP.put("subjectCategory",SubjectCategory.class);
        POJO_CLASS_MAP.put("subject",Subject.class);
    }

    private ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/searchAll/{type}")
    @ApiOperation("查询字典的所有数据")
    public RestResult searchAll(@PathVariable("type") String type) {
        if (!check(type)) {
            return RestResult.failure("数据字典类名出错，请检查");
        }
        return dictionaryProcessor.executeDictionaryMethod(type,"searchAll",null,null);
    }

    @GetMapping("/select/{type}")
    @ApiOperation("通过主键查询")
    public RestResult select(@PathVariable("type") String type,
                             @RequestParam("primaryId") Integer primaryId) {
        if (!check(type)) {
            return RestResult.failure("数据字典类名出错，请检查");
        }
        return dictionaryProcessor.executeDictionaryMethod(type,"selectByPrimaryKey",primaryId,Integer.class);
    }

    @GetMapping("/delete/{type}")
    @ApiOperation("删除字典数据")
    public RestResult delete(@PathVariable("type") String type,
                             @RequestParam("primaryId") Integer primaryId) {
        if (!check(type)) {
            return RestResult.failure("数据字典类名出错，请检查");
        }

        return dictionaryProcessor.executeDictionaryMethod(type,"deleteByPrimaryKey",primaryId,Integer.class);
    }

    @PostMapping("/insert/{type}")
    @ApiOperation("向字典中插入")
    public RestResult insert(@PathVariable("type") String type,
                             @RequestBody JSONObject jsonObject) {
        if (!check(type)) {
            return RestResult.failure("数据字典类名出错，请检查");
        }
        Object data = jsonObject.get("data");
        Object param = getDictionaryParam(type, data);
        Class dictionaryClass = getDictionaryClass(type);
        if (dictionaryClass==null || data==null) {
            return RestResult.success("数据出错，请重新检查");
        }
        return dictionaryProcessor.executeDictionaryMethod(type,"insertSelective",param,dictionaryClass);
    }

    @PostMapping("/update/{type}")
    @ApiOperation("修改字典数据")
    public RestResult update(@PathVariable("type") String type,
                             @RequestBody JSONObject jsonObject) {
        if (!check(type)) {
            return RestResult.failure("数据字典类名出错，请检查");
        }
        Object data = jsonObject.get("data");
        Object param = getDictionaryParam(type, data);
        Class dictionaryClass = getDictionaryClass(type);
        if (dictionaryClass==null || data==null) {
            return RestResult.success("数据出错，请重新检查");
        }
        return dictionaryProcessor.executeDictionaryMethod(type,"updateByPrimaryKeySelective",param,dictionaryClass);
    }

    private Object getDictionaryParam(String type,Object data) {
        Class aClass = POJO_CLASS_MAP.get(type);
        Object value = objectMapper.convertValue(data, aClass);
        return value;
    }

    private Class getDictionaryClass(String type) {
        return POJO_CLASS_MAP.get(type);
    }

    private boolean check(String type) {
        if (POJO_CLASS_MAP.get(type)==null) {
            return false;
        }
        return true;
    }

}
